package com.codename1.samples;

import com.codename1.components.SpanLabel;
import static com.codename1.ui.CN.*;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
import com.codename1.ui.layouts.BoxLayout;
import com.codename1.io.NetworkEvent;
import com.codename1.ui.Container;
import com.codename1.ui.Tabs;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.util.UITimer;

/**
 * This file was generated by <a href="https://www.codenameone.com/">Codename
 * One</a> for the purpose of building native mobile applications using Java.
 */
public class TabsAnimationSample2957 {

    private Form current;
    private Resources theme;

    public void init(Object context) {
        // use two network threads instead of one
        updateNetworkThreadCount(2);

        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature
        Log.bindCrashProtection(true);

        addNetworkErrorListener(err -> {
            // prevent the event from propagating
            err.consume();
            if (err.getError() != null) {
                Log.e(err.getError());
            }
            Log.sendLogAsync();
            Dialog.show("Connection Error", "There was a networking error in the connection to " + err.getConnectionRequest().getUrl(), "OK", null);
        });
    }

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        Form hi = new Form("Tabs", new BorderLayout());
        Tabs t = new Tabs();
        SpanLabel t1 = new SpanLabel("Blue");
        t1.getAllStyles().setBgColor(0xff);
        t1.getAllStyles().setBgTransparency(255);
        Container cont1 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        cont1.add(t1);
        SpanLabel t2 = new SpanLabel("Green");
        t2.getAllStyles().setBgColor(0xff00);
        t2.getAllStyles().setBgTransparency(255);
        Container cont2 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        cont2.add(t2);
        SpanLabel t3 = new SpanLabel("Red");
        t3.getAllStyles().setBgColor(0xff0000);
        t3.getAllStyles().setBgTransparency(255);
        Container cont3 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        cont3.add(t3);
        t.addTab("Blue", cont1);
        t.addTab("Green", cont2);
        t.addTab("Red", cont3);

        UITimer.timer(500, true, hi, () -> {
            int idx = t.getSelectedIndex() + 1;
            if (idx >= t.getTabCount()) {
                idx = 0;
            }
            t.setSelectedIndex(idx, true);
        });

        hi.add(CENTER, t);
        hi.show();
    }

    public void stop() {
        current = getCurrentForm();
        if (current instanceof Dialog) {
            ((Dialog) current).dispose();
            current = getCurrentForm();
        }
    }

    public void destroy() {
    }

}
